﻿
using System.Windows;

using Microsoft.Maps.MapControl;
using System.Windows.Media;
using System;
using MyFloridaHouse.libSvOpenDistricts.MapTools;


namespace MyFloridaHouse.libSvOpenDistricts.ViewModel
{

    public partial class ViewModelBase
    {
        //todo: move map defaults to config file when config function available
        public Location mapCenterDefault = new Location(28.0, -84.0);
        public const double maxMapZoomLevel = 20;


        private Map _myXamlMainMap;
        public Map myXamlMainMap
        {
            get { return _myXamlMainMap; }
            set { if (_myXamlMainMap != value) { _myXamlMainMap = value; NotifyPropertyChanged("myXamlMainMap"); } }
        }


        private bool _IsShowEdgesChecked;
        public bool IsShowEdgesChecked
        {
            get { return _IsShowEdgesChecked; }
            set { if (_IsShowEdgesChecked != value) { _IsShowEdgesChecked = value; NotifyPropertyChanged("IsShowEdgesChecked"); } }
        }

        private string _findStatus;
        public string findStatus
        {
            get { return _findStatus; }
            set { if (_findStatus != value) { _findStatus = value; NotifyPropertyChanged("findStatus"); } }
        }

        private string _mapStatus;
        public string mapStatus
        {
            get { return _mapStatus; }
            set { if (_mapStatus != value) { _mapStatus = value; NotifyPropertyChanged("mapStatus"); } }
        }

        private bool _showRoadMap;
        public bool showRoadMap
        {
            get { return _showRoadMap; }
            set { if (_showRoadMap != value) { _showRoadMap = value; NotifyPropertyChanged("showRoadMap"); } }
        }

        private Location _mapCenter;
        public Location mapCenter
        {
            get { return _mapCenter; }
            set
            {
                if (_mapCenter != value)
                {
                    _mapCenter = value;
                    //_mapZoomLevel = LimitMapZoomLevel(value);
                    NotifyPropertyChanged("mapCenter");
                }
            }
        }

        private double _mapZoomLevel;
        public double mapZoomLevel
        {
            get { return _mapZoomLevel; }
            set
            {
                if (_mapZoomLevel != value)
                {
                    _mapZoomLevel = value;
                    NotifyPropertyChanged("mapZoomLevel");
                }
            }
        }

        public string displayMouseLatLon
        {
            get
            {
                if (mapMouseLatLon != null)
                {
                    return mapMouseLatLon.Latitude.ToString("F6") + " / " + mapMouseLatLon.Longitude.ToString("F6");
                }
                else
                {
                    return "";
                }
            }
        }

        private Location _mapMouseLatLon;
        public Location mapMouseLatLon
        {
            get { return _mapMouseLatLon; }
            set { if (_mapMouseLatLon != value) { _mapMouseLatLon = value; NotifyPropertyChanged("mapMouseLatLon"); NotifyPropertyChanged("displayMouseLatLon"); } }
        }

        private Location _mapLatLonNW;
        public Location mapLatLonNW
        {
            get { return _mapLatLonNW; }
            set { if (_mapLatLonNW != value) { _mapLatLonNW = value; NotifyPropertyChanged("mapLatLonNW"); } }
        }

        private Location _mapLatLonSE;
        public Location mapLatLonSE
        {
            get { return _mapLatLonSE; }
            set { if (_mapLatLonSE != value) { _mapLatLonSE = value; NotifyPropertyChanged("mapLatLonSE"); } }
        }

        private Point _mapViewPortNW;
        public Point mapViewPortNW
        {
            get { return _mapViewPortNW; }
            set { if (_mapViewPortNW != value) { _mapViewPortNW = value; NotifyPropertyChanged("mapViewPortNW"); } }
        }

        private Point _mapViewPortSE;
        public Point mapViewPortSE
        {
            get { return _mapViewPortSE; }
            set
            {
                if (_mapViewPortSE != value)
                {
                    _mapViewPortSE = value;
                    NotifyPropertyChanged("mapViewPortSE");
                    NotifyPropertyChanged("mapLongitudePerPixel");
                    NotifyPropertyChanged("mapLatitudePerPixel");
                }
            }
        }

        public double mapLongitudePerPixel
        {
            get 
            {
                if ((_mapLatLonNW == null) | (_mapLatLonSE == null) | (_mapViewPortSE == null) | (_mapViewPortNW == null))
                {
                    return 0.0;
                }
                else
                {
                    double Xrange = _mapViewPortSE.X - _mapViewPortNW.X;
                    double LONrange = _mapLatLonNW.Longitude - _mapLatLonSE.Longitude;
                    return (LONrange / Xrange);
                }
            }
            set { } 
        }

        public double mapLatitudePerPixel
        {
            get 
            {
                if ((_mapLatLonNW == null) | (_mapLatLonSE == null) | (_mapViewPortSE == null) | (_mapViewPortNW == null))
                {
                    return 0.0;
                }
                else
                {
                    double Yrange = _mapViewPortSE.Y - _mapViewPortNW.Y;
                    double LATrange = _mapLatLonNW.Latitude - _mapLatLonSE.Latitude;
                    return (LATrange / Yrange);
                }
            }
            set { } 
        }

        private bool _isEditConfig = false;
        public bool isEditConfig
        {
            get { return _isEditConfig; }
            set { if (_isEditConfig != value) { _isEditConfig = value; NotifyPropertyChanged("isEditConfig"); } }
        }

        private SolidColorBrush _mapBackgroundBrush = new SolidColorBrush(Colors.LightGray);
        public SolidColorBrush mapBackgroundBrush
        {
            get { return _mapBackgroundBrush; }
            set { if (_mapBackgroundBrush != value) { _mapBackgroundBrush = value; NotifyPropertyChanged("mapBackgroundBrush"); } }
        }


        public void MapDataRefresh()
        {
            foreach (var myLayer in mapLayerList)
            {
                myLayer.RefreshShapesAndDataInView();
            }
        }

        public void SetMapCenter(object parameter)
        {
            try
            {
                mapCenter = parameter as Location; ;
            }
            catch
            {
                //todo: Need to log this unlikely error
            }
        }


        // MINIMAP


        private double _miniMapZoomLevel = 4;
        public double miniMapZoomLevel
        {
            get { return _miniMapZoomLevel; }
            set
            {
                if (_miniMapZoomLevel != value)
                {
                    _miniMapZoomLevel = value;
                    NotifyPropertyChanged("miniMapZoomLevel");
                }
            }
        }

        public MapPanControl mapPanControl = new MapPanControl();
    }
}
